<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>用户管理</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      max-width: 500px;
      margin: 50px auto;
      padding: 20px;
    }

    .form-group {
      margin-bottom: 15px;
    }

    label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }

    input {
      width: 100%;
      padding: 8px;
      border: 1px solid #ddd;
      border-radius: 4px;
    }

    button {
      background: #007bff;
      color: white;
      padding: 10px 20px;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

    button:hover {
      background: #0056b3;
    }

    .result {
      margin-top: 20px;
      padding: 10px;
      border-radius: 4px;
    }

    .success {
      background: #d4edda;
      color: #155724;
      border: 1px solid #c3e6cb;
    }

    .error {
      background: #f8d7da;
      color: #721c24;
      border: 1px solid #f5c6cb;
    }
  </style>
</head>

<body>
  <h1>用户管理</h1>

  <form id="userForm">
    <div class="form-group">
      <label for="email">邮箱地址:</label>
      <input type="email" id="email" name="email" required>
    </div>

    <button type="submit">创建用户</button>
  </form>

  <div id="result"></div>

  <script>
    document.getElementById('userForm').addEventListener('submit', async function (e) {
      e.preventDefault();

      const email = document.getElementById('email').value;
      const resultDiv = document.getElementById('result');

      try {
        const response = await fetch('/api/users', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({ email: email })
        });

        const data = await response.json();

        if (response.ok) {
          resultDiv.innerHTML = `<div class="result success">${data.status}</div>`;
          document.getElementById('email').value = '';
        } else {
          resultDiv.innerHTML = `<div class="result error">创建失败: ${data.detail || '未知错误'}</div>`;
        }
      } catch (error) {
        resultDiv.innerHTML = `<div class="result error">请求失败: ${error.message}</div>`;
      }
    });
  </script>
</body>

</html>